#include "mainwidget.h"

#include "context_widget.h"
#include "drag_widget.h"
#include "enter_leave_widget.h"
#include "key_widget.h"
#include "paint_widget.h"
#include "press_move_release_widget.h"
#include "propagate_widget.h"
#include "timer_widget.h"

#include<QHBoxLayout>
#include <QPushButton>
#include<QVBoxLayout>

MainWidget::MainWidget(QWidget *parent): QWidget(parent)
{

    // 1. 整体采用水平布局
    QHBoxLayout* horizontalLayout = new QHBoxLayout(this);
    horizontalLayout->setSpacing(0); // 布局中各个子控件的间距为0
    horizontalLayout->setContentsMargins(0,0,0,0); // 布局内容与控件边缘的间距

    // 2. 左侧导航窗体
    navWidget = new QWidget(this);
    navWidget->setMinimumWidth(400);
    QVBoxLayout* leftLayout = new QVBoxLayout(navWidget);
    leftLayout->setSpacing(0);
    leftLayout->setContentsMargins(0,0,0,0);

    horizontalLayout->addWidget(navWidget);


    // 3. 右侧主体布局
    mainWidget = new QWidget(this);
    QVBoxLayout* rightLayout = new QVBoxLayout(mainWidget);
    rightLayout->setSpacing(0);
    rightLayout->setContentsMargins(0,0,0,0);

    stackedWidget = new QStackedWidget(mainWidget);
    rightLayout->addWidget(stackedWidget);

    horizontalLayout->addWidget(mainWidget);

    initNav();

    initMain();
}

MainWidget::~MainWidget() {}

/** 初始化左侧的导航按钮
 * @brief MainWidget::initNav
 */
void MainWidget::initNav()
{
    QStringList names;
    names.append("鼠标进入/离开");
    names.append("鼠标按下/移动/释放");
    names.append("键盘事件");
    names.append("定时器事件");
    names.append("拖动事件");
    names.append("绘图事件");
    names.append("右键事件");
    names.append("总结：事件传递流程");

    btnGroup = new QButtonGroup(this);

    for(int i = 0; i < names.count(); i++) {
        QPushButton* btn = new QPushButton();

        btn->setMinimumHeight(60);
        btn->setCheckable(true);
        btn->setText(QString("%1. %2").arg(i + 1, 2, 10, QChar('0')).arg(names.at(i)));

        btnGroup->addButton(btn, i);

        navWidget->layout()->addWidget(btn);

        // 槽函数
        connect(btn, &QPushButton::clicked, this, &MainWidget::onButtonClicked);
    }

    // 设置按钮的样式
    navWidget->setStyleSheet(R"(
            QPushButton {
                font-size: 20px;
                text-align: left
            }
        )");

    // 默认选中第一项
    btnGroup->button(0)->click();
}

/**
 * @brief MainWidget::initMain
 */
void MainWidget::initMain()
{
    stackedWidget->addWidget(new EnterLeaveWidget());
    stackedWidget->addWidget(new PressMoveReleaseWidget());
    stackedWidget->addWidget(new KeyWidget());
    stackedWidget->addWidget(new TimerWidget());
    stackedWidget->addWidget(new DragWidget());
    stackedWidget->addWidget(new PaintWidget());
    stackedWidget->addWidget(new ContextWidget());
    stackedWidget->addWidget(new PropagateWidget());
}

/** 槽函数：按钮的点击事件
 * @brief MainWidget::onButtonClicked
 */
void MainWidget::onButtonClicked()
{
    int id = btnGroup->checkedId();

    stackedWidget->setCurrentIndex(id);
}


























































